package com.lanmao.blog.controller;

import com.lanmao.blog.entity.Music;
import com.lanmao.blog.service.MusicService;
import com.lanmao.blog.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/music")
public class MusicController {
    static HashMap<String,String> map=new HashMap<>();
    @Autowired
    private MusicService musicService;
    /**
     * 获取mp3播放地址
     * @param rid
     * @return
     */
    @GetMapping("/getrid")
    public R getrid( String rid) {
        try{
            String url = musicService.mp3url(rid);
            return R.ok().data("url",url).data("rid",rid);
        }
        catch (Exception e){
            System.out.println(e);
        }

        return R.error();
    }
    /**
     * 获取mp4播放地址
     * @param rid
     * @return
     */
    @GetMapping("/getmp4")
    public R getmp4(String rid) {
        try {
            String url = musicService.mp4url(rid);

            return R.ok().data("url",url).data("rid",rid);
        }catch (Exception e){
            System.out.println("数据请求失败!");
        }
        return R.error();
    }

    /**
     * 获取歌曲名字
     * @param rid
     * @return
     */
    @GetMapping("/getmp3name")
    public R getmp3name(String rid) {
        String name = map.get(rid);
        return R.ok().data("name",name);
    }
    /**
     * 热歌收录100首
     * @return
     * @throws Exception
     */
    @RequestMapping("/paihangb")
    public R paihangb() throws Exception {
        List<Music> musicList = musicService.paihangb();
        for (int i = 0; i < musicList.size(); i++) {
            String rid = musicList.get(i).getRid();
            if (!map.containsKey(rid)){
                map.put(rid,musicList.get(i).getName());
            }

        }
        return R.ok().data("musicList",musicList).data("total",100);
    }

}
